package com.liukai.todo_android.presenter;

import com.liukai.todo_android.common.utils.RetrofitManager;
import com.liukai.todo_android.common.api.UserApi;
import com.liukai.todo_android.common.base.BasePresenter;
import com.liukai.todo_android.call_back.IValidateCallBack;
import com.liukai.todo_android.entity.Resp;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * @author ：liukai
 * @date ：Created in 2021/8/9 10:02 上午
 * @description：校验是否成功登录的请求
 * @modified By：
 * @version: $
 */
public class ValidatePresenter extends BasePresenter<IValidateCallBack> {

    /**
     * 校验是否已经登录
     */
    public void validateIsLogin() {
        Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
        UserApi userApi = retrofit.create(UserApi.class);
        Call<Resp> validateTask = userApi.validateIsLogin();
        validateTask.enqueue(new Callback<Resp>() {
            @Override
            public void onResponse(Call<Resp> call, Response<Resp> response) {
                int code = response.code();
                System.out.println("response==>" + response.toString());
                if (code == HttpURLConnection.HTTP_OK) {
                    Resp body = response.body();
                    if (body.getCode() == 200){
                        mCallBack.validateIsLoginSuccess(body);
                    }else{
                        mCallBack.validateIsLoginError(body);
                    }
                } else {
                    mCallBack.validateIsLoginError(Resp.err("未登录，请重新登录"));
                }
            }

            @Override
            public void onFailure(Call<Resp> call, Throwable t) {
                mCallBack.validateIsLoginError(Resp.err("请检查网络是否连接"));
            }
        });

    }

}
